﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Public Class Common
   Public Shared Function GetDataMemberValueFromEnum(val As [Enum], enumType As Type) As String
         Dim member() As Reflection.MemberInfo = enumType.GetMember(val.ToString)
         If member.Count > 0 Then
            Dim attr As Runtime.Serialization.EnumMemberAttribute() = DirectCast(member(0).GetCustomAttributes(GetType(Runtime.Serialization.EnumMemberAttribute), False), Runtime.Serialization.EnumMemberAttribute())
            Return attr(0).Value
         End If
   
      Return String.Empty
   End Function
   Public Shared Function GetValueByDataMemberValue(Of T)(val As String) As T
      Dim enumType As Type = GetType(T)
      val = val.ToLowerInvariant
      Dim members() As Reflection.MemberInfo = enumType.GetMembers
      For Each member As Reflection.MemberInfo In members
         If member.ReflectedType Is enumType Then
            Dim attr As Runtime.Serialization.EnumMemberAttribute() = DirectCast(member.GetCustomAttributes(GetType(Runtime.Serialization.EnumMemberAttribute), False), Runtime.Serialization.EnumMemberAttribute())
            If attr.Count > 0 Then
               Dim attrVal As String = attr(0).Value
               If attrVal.ToLowerInvariant.Equals(val) Then
                  Return DirectCast(enumType.GetField(member.Name).GetValue(Nothing), T)
               End If
            End If
         End If
      Next

   End Function
   Public Shared Function GetXMLRootName(objType As Type) As String
      Dim attr() As System.Xml.Serialization.XmlRootAttribute = DirectCast(objType.GetCustomAttributes(GetType(System.Xml.Serialization.XmlRootAttribute), False), System.Xml.Serialization.XmlRootAttribute())
      Return attr(0).ElementName
   End Function
   Public Shared Function GetXMLRootNamespace(objType As Type) As String
      Dim attr() As System.Xml.Serialization.XmlRootAttribute = DirectCast(objType.GetCustomAttributes(GetType(System.Xml.Serialization.XmlRootAttribute), False), System.Xml.Serialization.XmlRootAttribute())
      Return attr(0).Namespace
   End Function
   Public Shared Function GetItemCaseInsensitiveFromXML(doc As Xml.XmlDocument, name As String) As Xml.XmlNode
      name = name.ToLowerInvariant
      For Each child As Xml.XmlNode In doc.ChildNodes
         If GetNodeNameWithoutPrefix(child).ToLowerInvariant.Equals(name) Then
            Return child
         End If
      Next
      Return Nothing
   End Function

   Public Shared Function GetNodeNameWithoutPrefix(node As Xml.XmlNode) As String
      Dim name As String = node.Name
      Dim nameParts() As String = name.Split(":"c)
      If nameParts.Count = 2 Then
         Return nameParts(1)
      End If
      Return nameParts(0)
   End Function

   Public Shared Function GetNodeNamePrefix(node As Xml.XmlNode) As String
      Dim name As String = node.Name
      Dim nameParts() As String = name.Split(":"c)
      If nameParts.Count = 2 Then
         Return nameParts(0)
      End If
      Return Nothing
   End Function

   Public Shared Function ParseBase64(doc As Xml.XmlDocument) As Xml.XmlDocument
      For Each node As Xml.XmlNode In doc.ChildNodes
         If GetNodeNameWithoutPrefix(node).ToLowerInvariant.Equals("binary") Then
            Dim retVal As New Xml.XmlDocument()
            retVal.LoadXml(Text.Encoding.UTF8.GetString(Convert.FromBase64String(node.InnerText)))
            Return retVal
         End If
      Next
      Return doc
   End Function


End Class
